//
//  CollectionViewController.m
//  UI-21-豆豆豆瓣
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Wang Hao. All rights reserved.
//

#import "CollectionViewController.h"
#import "AppTools.h"
#import "ListCollectionViewCell.h"
#import "Movie.h"
#import "MovieDetailViewController.h"
#import "MBProgressHUD.h"

@interface CollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *myCollection;

@property (nonatomic, retain) NSMutableArray *movieArray;
@property (nonatomic, retain) MBProgressHUD *hup;



@end


@implementation CollectionViewController
- (void)dealloc
{
    [_myCollection release];
    [_movieArray release];
    [_hup release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 567/4+17.5);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    //设置最小行间距
    flowLayout.minimumInteritemSpacing = 5;
    //设置最小列间距
    flowLayout.minimumLineSpacing = 20;
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 667-111) collectionViewLayout:flowLayout];
    self.myCollection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myCollection];
    [_myCollection release];
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    [self.myCollection registerClass:[ListCollectionViewCell class] forCellWithReuseIdentifier:@"itm"];
    self.hup = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hup show:YES];
    self.hup.labelText = @"loading.....";
    self.movieArray = [NSMutableArray array];
    [self getWoring];

    //搜索框
    
    
    
    
}
-(void)getWoring{
    [AppTools getUrl:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            
            for (NSMutableDictionary *dic in array) {
                Movie *movie = [[Movie alloc] init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArray addObject:movie];
                [movie release];
            }
            [self.myCollection reloadData];
            [self.hup hide:YES];
        }else{
            
            NSLog(@"请求失败");
        }
        
        
    }];

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.movieArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *itmIndent = @"itm";
    ListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itmIndent forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    cell.movie = [self.movieArray objectAtIndex:indexPath.row];
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MovieDetailViewController *movDetail = [[MovieDetailViewController alloc] init];
    Movie *mov = [self.movieArray objectAtIndex:indexPath.row];
    movDetail.movie = mov;
    [self.navigationController pushViewController:movDetail animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
